package com.bbtShop.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbtShop.core.model.Product;
import com.bbtShop.core.model.SysMenu;
import com.bbtShop.core.model.SysRoleMenu;
import com.bbtShop.core.model.SysRoleMenuVo;
import com.bbtShop.dao.SysMenuMapper;
import com.bbtShop.dao.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;

/**
 * @author : manke
 * @Description
 * @Date: 2020/10/31 19:39
 */
@Service
public class SysMenuService extends ServiceImpl<SysRoleMenuMapper,SysRoleMenu> {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    public void addUpdate(SysMenu sysMenu) {
        if (sysMenu.getId() == null) {
            sysMenu.setCreateTime(new Date());
            sysMenuMapper.insert(sysMenu);
        } else {
            sysMenuMapper.updateById(sysMenu);
        }

    }

    public void roleBindMenu(SysRoleMenuVo sysRoleMenuVo) {
        LambdaUpdateWrapper<SysRoleMenu> lambda = new UpdateWrapper<SysRoleMenu>().lambda();
        lambda.eq(SysRoleMenu::getRoleId,sysRoleMenuVo.getRoleId());
        sysRoleMenuMapper.delete(lambda);
        LinkedList<SysRoleMenu> sysRoleMenus = new LinkedList<>();
        for (Integer menu : sysRoleMenuVo.getMenus()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRoleMenuVo.getRoleId());
            sysRoleMenu.setMenuId(menu);
            sysRoleMenus.add(sysRoleMenu);
        }
        this.saveBatch(sysRoleMenus);



    }
}
